package com.thz.houserental.domain.house;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thz.houserental.domain.reservation.Reservation;
import com.thz.houserental.domain.reservation.ReservationService;
import com.thz.houserental.infrastructure.auth.AuthUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/house")
@Tag(name = "房源管理")
public class HouseController {
    private final HouseService houseService;
    private final ReservationService reservationService;

    public HouseController(HouseService houseService, ReservationService reservationService) {
        this.houseService = houseService;
        this.reservationService = reservationService;
    }

    @PostMapping("/add")
    @Operation(summary = "添加房源信息")
    public House addNewHouse(@RequestBody @Valid HouseDto house) {
        house.setId(null);
        return this.houseService.addHouse(house);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除房源信息")
    @Transactional
    public void deleteHouse(@RequestParam @Parameter(description = "房屋id") String houseId) {
        this.houseService.deleteHouse(houseId);
        //删除关联信息
        QueryWrapper<Reservation> reservationQueryWrapper = new QueryWrapper<>();
        reservationQueryWrapper.lambda()
                .eq(Reservation::getHouseId, houseId);
        this.reservationService.remove(reservationQueryWrapper);
    }

    @GetMapping("/list")
    @Operation(summary = "分页查询房源")
    public Page<HouseVo> queryPage(House house,
                                   @RequestParam(name = "minPrice", required = false) Integer minPrice,
                                   @RequestParam(name = "maxPrice", required = false) Integer maxPrice,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<House> houseQueryWrapper = new QueryWrapper<>();
        houseQueryWrapper.lambda()
                .like(StrUtil.isNotEmpty(house.getLocation()), House::getLocation, house.getLocation())
                .eq(StrUtil.isNotEmpty(house.getLeaseTerm()), House::getLeaseTerm, house.getLeaseTerm())
                .eq(StrUtil.isNotEmpty(house.getType()), House::getType, house.getType())
                .eq(StrUtil.isNotEmpty(house.getHouseType()), House::getHouseType, house.getHouseType())
                .ge(minPrice != null, House::getPrice, minPrice)
                .le(maxPrice != null, House::getPrice, maxPrice)
                .eq(house.getOnline() != null, House::getOnline, house.getOnline());

        return this.houseService.queryPage(houseQueryWrapper, new Page<>(pageNo, pageSize));
    }

    @GetMapping("/myHouse")
    @Operation(summary = "我的房源")
    public Page<House> getMyHouses(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        QueryWrapper<House> houseQueryWrapper = new QueryWrapper<>();
        houseQueryWrapper.lambda().eq(House::getLandlord, AuthUtil.getCurrentUserId());
        return this.houseService.page(new Page<>(pageNo, pageSize), houseQueryWrapper);
    }

    @GetMapping("/get/{houseId}")
    @Operation(summary = "通过houseId获取房屋详情")
    public HouseVo getOne(@PathVariable String houseId) {
        return this.houseService.getDetail(houseId);
    }

    @PutMapping("/update")
    @Operation(summary = "更新房源信息")
    @Transactional
    public void updateHouse(@RequestBody @Valid HouseDto houseDto) {
        this.houseService.updateHouse(houseDto);
    }

    @PutMapping("/{houseId}/offline")
    @Operation(summary = "房源下线")
    public void offline(@PathVariable String houseId) {
        this.houseService.offLine(houseId);
    }
}
